<?php
require 'tu.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    foreach ($_FILES as $info) {
        $tmp_file = $info['tmp_name'];

        if (in_array($info['type'], array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'))
            && $info['size'] / 1024 / 1024 <= 4) {
            $fh = fopen($tmp_file, "rb");
            $data = fread($fh, filesize($tmp_file));
            fclose($fh);
            
            $key = saveImage($info,$data);
            
            echo $key; 
        }
        else {
            echo 'error'; 
        }
    }
}
else {
    $url = parse_url(GetCurUrl());
    $key = str_replace('/','',$url['path']);
    if ($key) {
        getImage($key);
    }
    else {
        require 'index.tpl';
    }
}
